<template>
  <div class="confirmationPage">
    <div class="divHead">
      <div class="divTitle">
        <ArrowLeft @click="router.back()" style="width: 5vw; height: 5vh;" />
        <span>订单确认</span>
      </div>
    </div>

    <el-card shadow="always" style="width: 97%; margin-left: 50%; transform: translate(-50%);margin-top: 10px;">
      <div class="addressBox">
        <div class="addressLabel">{{ addressInfo.detail }}</div>
        <div class="userInfo">
          <div class="name">{{ addressInfo.consignee }} {{ addressInfo.sex === 1 ? '先生' : '女士' }}</div>
          <div class="phone">13100000000</div>
        </div>
      </div>
      <el-divider style="margin-top: 15px;" />

      <div class="estimatedTime">预计xx送达</div>
    </el-card>

    <el-card shadow="always" style="width: 97%; margin-left: 50%; transform: translate(-50%);margin-top: 10px;">
      <div class="orderBox">
        <div class="orderLabel">订单明细</div>
        <!-- <el-divider style="margin-top: 15px;" /> -->
        <div class="orderItem" v-for="(item, index) in shoppingCartData" :key="index">
          <el-divider style="margin: 10px 0px;" />
          <div class="contentBox">
            <div class="leftBox">
              <el-image style="width: 70px; height: 70px" :src="item.url" fit="cover" />

              <div class="textBox">
                <div class="name">{{ item.name }}</div>
                <div class="number">x {{ item.number }}</div>
              </div>
            </div>

            <div class="price">￥ {{ item.amount * (item.number as number) }}</div>
          </div>

        </div>
      </div>
    </el-card>

    <el-card shadow="always" style="width: 97%; margin-left: 50%; transform: translate(-50%);margin-top: 10px;">
      <div class="remarkBox">
        <div class="remarkLabel">备注</div>
        <el-divider style="margin: 15px 0px 0px 0px; " />
        <el-input v-model="remark" :autosize="{ minRows: 3, maxRows: 5 }" type="textarea" placeholder="请输入备注"
          style="border: none !important;" />
      </div>
    </el-card>

    <div class="buttom">
      <el-image class="elImage" :src="require('@/assets/cart_active.png')" fit="cover" />

      <div class="totalValue">￥ {{ totalMoney }}</div>

      <el-button round type="primary" color="#ffc500" class="elButton" @click="submit">去支付</el-button>
    </div>
  </div>
</template>

<script setup lang="ts" name="confirmationPage">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { ArrowLeft } from '@element-plus/icons-vue'
import { getAddressList, shoppingCartList, submitOrder } from '@/api/index'
import { ElMessage } from 'element-plus';
import { Iaddress, IShoppingCart } from './interface'
import { getImg } from '@/utils/index'
const router = useRouter()

const remark = ref<string>('')

onMounted(() => {
  getAddress()
  getShoppingCartList()
})

const addressInfo = ref<Iaddress>({
  consignee: '',
  sex: -1,
  phone: '',
  detail: '',
  label: '',
  id: '',
  isDefault: -1,
})
const getAddress = async () => {
  try {
    const { data } = await getAddressList<Iaddress[]>()
    addressInfo.value = data[0]
  } catch (error) {
    ElMessage.error(error as string)
  }
}


const shoppingCartData = ref<IShoppingCart[]>()
const totalMoney = ref<number>(0)

const getShoppingCartList = async () => {
  try {
    const { data } = await shoppingCartList<IShoppingCart[]>()
    let i = 0
    data.forEach(async item => {
      item.url = await getImg(item.image)
      totalMoney.value += item.amount * (item.number as number)
      i++
      if (i === data.length) {
        shoppingCartData.value = data
      }
    })

  } catch (error) {
    ElMessage.error(error as string)
  }
}

const submit = async () => {
  const params = {
    addressBookId: addressInfo.value.id,
    remark: remark.value,
    payMethod: 2
  }
  
  try {
    const {data} = await submitOrder<string>(params)
    ElMessage.success(data)
  } catch (error) {
    ElMessage.error(error as string)
  }
}
</script>

<style scoped lang="scss">
.confirmationPage {
  box-sizing: border-box;
  width: 100%;
  height: 100vh;
  background-color: #f3f2f7;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 60px;

  .divHead {
    width: 100%;
    height: 10%;
    opacity: 1;
    background: #333333;
    box-sizing: border-box;
    padding-left: 3vw;
    padding-right: 3vw;

    .divTitle {
      // height: 50%;
      padding-top: 5%;
      display: flex;
      align-items: center;
      text-align: center;
      font-size: 23px;
      color: #fff;

      span {
        flex: 1;
      }
    }
  }

  .addressBox {
    color: darkgray;

    .addressLabel {
      font-size: 20px;
      font-weight: 600;
      color: #333333;
    }

    .userInfo {
      display: flex;
      align-items: baseline;
      margin-top: 5px;

      .name {
        margin-right: 10px;
      }
    }
  }

  .estimatedTime {
    color: darkgray;
  }

  .orderBox {
    color: darkgray;

    .orderLabel {
      font-size: 20px;
      font-weight: 600;
      color: #333333;
    }

    .orderItem {
      .contentBox {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .leftBox {
          display: flex;

          .textBox {
            display: flex;
            flex-direction: column;
            margin-left: 10px;

            .name {
              color: black;
              font-size: 17px;
              font-weight: 700;
              margin-bottom: 18px;
            }
          }
        }

        .price {
          font-size: 18px;
          color: red;
          font-weight: 700;
        }
      }
    }
  }

  .remarkBox {
    .el-textarea {
      --el-input-text-color: var(--el-text-color-regular);
      --el-input-border: var(--el-border);
      --el-input-hover-border: #fff;
      --el-input-focus-border: #fff;
      --el-input-transparent-border: 0 0 0 1px transparent inset;
      --el-input-border-color: #fff;
      --el-input-border-radius: #fff;
      --el-input-bg-color: var(--el-fill-color-blank);
      --el-input-icon-color: var(--el-text-color-placeholder);
      --el-input-placeholder-color: var(--el-text-color-placeholder);
      --el-input-hover-border-color: #fff;
      --el-input-clear-hover-color: var(--el-text-color-secondary);
      --el-input-focus-border-color: #fff;
    }
  }

  .buttom {
    z-index: 5;
    width: 90%;
    height: 40px;
    margin-left: 50%;
    margin-top: 10px;
    transform: translate(-50%);
    border-radius: 50px;
    background: #333333;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: absolute;
    bottom: 10px;

    .elImage {
      position: absolute;
      top: -13px;
      left: 10px;
      height: 50px;
    }

    .totalValue {
      color: #fff;
      margin-left: 100px;
    }

    .elButton {
      width: 100px;
      height: 90%;
      margin-right: 3px;
    }
  }
}
</style>